#include "StdAfx.h"
#include "DisplayModel.h"

CDisplayModel::CDisplayModel(const BYTE* pModelData)
 : m_nRow(0)
 , m_nCol(0)
 , m_nDisplayNumber(0)
{
	BuildDisplayModelRectArray(pModelData);
}

CDisplayModel::~CDisplayModel(void)
{
}

void CDisplayModel::BuildDisplayModelRectArray(const BYTE* pModelData)
{
	int x = 0;
	int nCount = 0;
	for(int i = 0; x != 0 || pModelData[i] != 0; ++i)
	{
		if(pModelData[i] == 0)
			x = 0;
		else
			++x;

		if(pModelData[i] > nCount)
			nCount = pModelData[i];
	}

	m_DisplayModelRectArray.resize(nCount);
	for(int i = 0; i < nCount; ++i)
	{
		m_DisplayModelRectArray[i].SetRectEmpty();
	}

	int y = 0;
	for(int i = 0; x != 0 || pModelData[i] != 0; ++i)
	{
		if(pModelData[i] == 0)
		{
			m_nCol = x;
			x = 0;
			++y;
		}
		else
		{
			CRect& rect = m_DisplayModelRectArray[pModelData[i] - 1];

			if(rect.IsRectEmpty())
			{
				rect.SetRect(x, y, x+1, y+1);
			}
			else
			{
				if(x < rect.left)
					rect.left = x;
				else if(x >= rect.right)
					rect.right = x+1;

				if(y < rect.top)
					rect.top = y;
				else if(y >= rect.bottom)
					rect.bottom = y+1;
			}
			++x;
		}
	}

	m_nRow = y;
	m_nDisplayNumber = static_cast<int>(m_DisplayModelRectArray.size());
}
